<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="上级部门" prop="parentName">
        <el-popover
          ref="cateListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="cateList"
            :props="cateListTreeProps"
            node-key="id"
            ref="cateListTree"
            @current-change="cateListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:cateListPopover :readonly="true" placeholder="点击选择上级分类"
                  class="menu-list__input"></el-input>
      </el-form-item>
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="分类名称"></el-input>
          </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button  @click="visible = false">取消</el-button>
      <el-button  type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {treeDataTranslate} from '@/utils'

  export default {
    data () {
      return {
        visible: false,
        loading: false,
        cateList: [],
        cateListTreeProps: {
          label: 'name',
          children: 'children'
        },
        dataForm: {
          id: 0,
          name: '',
          parentName: '',
          sort: 100,
          parentId: 0
        },
        dataRule: {
          name: [
            {required: true, message: '分类名称不能为空', trigger: 'blur'}
          ],
          parentName: [
            {required: true, message: '上级分类不能为空', trigger: 'blur'}
          ],
          sort: [
            {required: true, message: '排序不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$http({
          url: this.$http.adornUrl('/exam-admin/ques/cate/select'),
          method: 'post'
        }).then(res => {
          this.cateList = treeDataTranslate(res.data, 'id')
        }).then(res => {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/exam-admin/ques/cate/info/${this.dataForm.id}`),
                method: 'post'
              }).then(data => {
                if (data && data.code === 200) {
                  this.dataForm.name = data.data.name
                  this.dataForm.sort = data.data.sort
                  this.dataForm.parentId = data.data.parentId
                  this.cateListTreeSetCurrentNode()
                }
              })
            } else {
              this.cateListTreeSetCurrentNode()
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.loading = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/exam-admin/ques/cate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'sort': this.dataForm.sort,
                'parentId': this.dataForm.parentId
              }
            }).then(data => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.error(data.msg)
              }
              this.loading = false
            })
          }
        })
      },
      // 菜单树选中
      cateListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.name
      },
      // 菜单树设置当前选中节点
      cateListTreeSetCurrentNode () {
        this.$refs.cateListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.cateListTree.getCurrentNode() || {})['name']
      }
    }
  }
</script>
